/**
 * 
 */
package net.ddns.heroli.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.model.json.AjaxJson;
import cn.com.gzheroli.core.common.model.json.TreeGrid;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.common.service.HeroService;
import cn.com.gzheroli.core.common.service.SystemService;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.NameHandler;
import cn.com.gzheroli.core.util.RequestUtil;
import cn.com.gzheroli.util.GenSqlUtil;
import net.ddns.heroli.pojo.TBModel;
import net.ddns.heroli.pojo.TBParaAttr;
import net.ddns.heroli.pojo.TBSProType;

/**  
* 类名: ModelController  
* 描述: TODO(这里用一句话描述这个类的作用)  
* 作者: 李长荣 lichang_20011@163.com  
* 日期: 2017年12月20日  下午2:08:39
*@version V1.0  
*    
*/
@Controller
@RequestMapping("/api/modelController")
public class ModelController {
	private static final Logger logger = LoggerFactory.getLogger(ModelController.class);
	@Autowired
	private CommonService commonService;
	@Autowired
	private HeroService heroService;
	@Autowired
    private SpringRedisCache cache;
	@Autowired
	private SystemService systemService;
	@RequestMapping(value = "get")
	@ResponseBody
	public AjaxJson get(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		TBModel model=commonService.get(TBModel.class, id);
		j.setObj(model);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "checkModel")
	@ResponseBody
	public AjaxJson checkModel(String id,String checkMsg, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		Map reqMap=new HashMap();
		reqMap.put("updateBy", user.getId());
		reqMap.put("updateTime", new Date());
		reqMap.put("updateByName", user.getRealName());
		reqMap.put("id", id);
		reqMap.put("state", "1");
		reqMap.put("checkMsg", checkMsg);
		Integer ret=heroService.updateMap("T_B_MODEL", reqMap, null, TSUser.class);
		if(ret>0) {
			j.setSuccess(true);
			j.setMsg("审核成功!");
		}else {
			j.setSuccess(false);
			j.setMsg("审核失败!");
		}
		
		return j;
	}
	@RequestMapping(value = "del")
	@ResponseBody
	public AjaxJson del(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		TBModel model=commonService.get(TBModel.class, id);
		model.setUpdateBy(user.getId());
		model.setUpdateTime(new Date());
		model.setUpdateByName(user.getRealName());
		model.setStatus("0");
		commonService.update(model);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "save")
	@ResponseBody
	public  AjaxJson save(TBModel model, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		if(model.getId()==null){
			model.setUpdateBy(user.getId());
			model.setUpdateTime(new Date());
			model.setUpdateByName(user.getRealName());
			model.setStatus("1");
			commonService.save(model);
		}else{
			model.setUpdateBy(user.getId());
			model.setUpdateTime(new Date());
			model.setUpdateByName(user.getRealName());
			commonService.update(model);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "update")
	@ResponseBody
	public  AjaxJson update( HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		String id=request.getParameter("id")==null?"":request.getParameter("id");
		TSUser user=client.getUser();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("保存失败!,编号不能为空");
			return j;
		}else{
			Map reqMap=RequestUtil.convertParameterToMap(request);
			reqMap.put("updateBy", user.getId());
			reqMap.put("updateTime", new Date());
			reqMap.put("updateByName", user.getRealName());
			heroService.updateMap("T_B_MODEL", reqMap, null, TBModel.class);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "list")
	@ResponseBody
	public  AjaxJson  list(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		//Client client=cache.getCache(request.getSession().getId(), Client.class);
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			//return null;
		}else{
			TSUser user=client.getUser();
		}
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("brandId"))
		conMap.put("brandId", "=");
		if(reqMap.containsKey("catId"))
			conMap.put("catId", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		if(reqMap.containsKey("desc"))
			conMap.put("desc", "like");
		if(reqMap.containsKey("catName"))
			conMap.put("catName", "like");
		Class clazz=TBModel.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
	/**
	 * 
	* @Title getGoodModelList 
	* @Description 商品可以选择的型号列表,带分页
	* @param @param request
	* @param @param response
	* @param @return    设定文件 
	* @return AjaxJson    返回类型 
	* @throws
	 */
	@RequestMapping(value = "getGoodModelList")
	@ResponseBody
	public  AjaxJson  getGoodModelList(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		//Client client=cache.getCache(request.getSession().getId(), Client.class);
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			//return null;
		}else{
			TSUser user=client.getUser();
		}
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		reqMap.put("state", "1");
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("brandId"))
		conMap.put("brandId", "=");
		if(reqMap.containsKey("catId"))
			conMap.put("catId", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		if(reqMap.containsKey("modelDesc"))
			conMap.put("modelDesc", "like");
		if(reqMap.containsKey("catName"))
			conMap.put("catName", "like");
		Class clazz=TBModel.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
}
